package ihm;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import metier.Entrainement;
import dao.DaoEntrainement;

@SuppressWarnings("serial")
public class FenetreGestionEntrainement extends JFrame implements ActionListener
{

        private JPanel contentPane;
        private JButton btnRetour;
        private JButton btnValider;
        private JLabel lblDateDentrainement;
        private JTextField txtHeureDeb;
        private JTextField txtMinDeb;
        private JLabel lblVeuillezRespecterCe;
        private JLabel lblSaisirHeureFin;
        private JTextField txtHeureFin;
        private JTextField txtMinFin;
        private JLabel label;
        
		@SuppressWarnings("rawtypes")
		private JComboBox cbJours;
		
        @SuppressWarnings("rawtypes")
		private JComboBox cbMois;
        
        @SuppressWarnings("rawtypes")
		private JComboBox cbAnnee;
      
        @SuppressWarnings("rawtypes")
		private JComboBox cbMoniteur;
        
        private Calendar calendrier;
    	private Vector<Entrainement> lesEntrainements = DaoEntrainement.getLesEntrainements();
        
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try
                                {
                                	FenetreGestionEntrainement frame = new FenetreGestionEntrainement();
                                	frame.setVisible(true);
                                } catch (Exception e) 
                                {
                                	e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the frame.
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
		public FenetreGestionEntrainement()
        {
        	setTitle("Ajout d'un entrainment");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 500, 370);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
               
                JLabel lblChoisirUnMoniteur = new JLabel("Choisir un moniteur :");
                lblChoisirUnMoniteur.setBounds(73, 40, 120, 14);
                contentPane.add(lblChoisirUnMoniteur);
               
                cbMoniteur = new JComboBox(dao.DaoAdherent.getLesAdherents());
                cbMoniteur.addActionListener(this);
                cbMoniteur.setBounds(216, 37, 186, 20);
                contentPane.add(cbMoniteur);

                JLabel lblSaisirHeureDebut = new JLabel("Heure de d\u00E9but :");
                lblSaisirHeureDebut.setBounds(73, 139, 139, 14);
                contentPane.add(lblSaisirHeureDebut);
               
                JLabel lblH = new JLabel("  :");
                lblH.setFont(new Font("Tahoma", Font.PLAIN, 15));
                lblH.setBounds(256, 168, 23, 20);
                contentPane.add(lblH);
               
                btnValider = new JButton("Valider");
                btnValider.addActionListener(this);
                btnValider.setBounds(384, 265, 90, 20);
                contentPane.add(btnValider);
               
                btnRetour = new JButton("Retour");
                btnRetour.addActionListener(this);
                btnRetour.setBounds(276, 265, 90, 20);
                contentPane.add(btnRetour);
                
                // ---------------------------------------
                //------------------------------------------
                //------------------------------------------------
                lblDateDentrainement = new JLabel("Date d'entra\u00EEnement :");
                lblDateDentrainement.setBounds(76, 77, 131, 14);
                contentPane.add(lblDateDentrainement);
                
            	cbJours = new JComboBox();
            	cbJours.setEditable(true);
        		cbJours.setModel(new DefaultComboBoxModel(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        		cbJours.setBounds(216, 68, 50, 22);
        		contentPane.add(cbJours);
        		
        		cbMois = new JComboBox();
        		cbMois.setEditable(true);
        		cbMois.setModel(new DefaultComboBoxModel(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}));
        		cbMois.setBounds(276, 68, 50, 22);
        		contentPane.add(cbMois);
        		
        		
        		cbAnnee = new JComboBox();
        		
        		// Remplissage de la comboBox automatiquement 
        		// de l'année système - 14 à l'année en cours (début de comboBox à 2000)
        		calendrier = Calendar.getInstance();
        		int year = calendrier.get(Calendar.YEAR);
        		int anneeCalcul=0;
        		String annee[] = new String[15];
        		for (int i = 0 ; i <= 14 ; i++)
        		{
        			anneeCalcul = (year - 14 ) + i;
        			annee[i] = String.valueOf(anneeCalcul);
        			//System.out.println(annee[i]);
        		}
        		cbAnnee.setModel(new DefaultComboBoxModel(annee));
        		
        		cbAnnee.setEditable(true);
        		cbAnnee.setBounds(332, 68, 70, 22);
        		contentPane.add(cbAnnee);
        		
        		// ---------------------------------------
                //------------------------------------------
                //------------------------------------------------
                txtHeureDeb = new JTextField();
                txtHeureDeb.setBounds(216, 142, 39, 20);
                contentPane.add(txtHeureDeb);
                txtHeureDeb.setColumns(10);
                
                txtMinDeb = new JTextField();
                txtMinDeb.setBounds(282, 142, 39, 20);
                contentPane.add(txtMinDeb);
                txtMinDeb.setColumns(10);
                
                lblVeuillezRespecterCe = new JLabel("Veuillez respecter le format suivant pour l'heure (hh:mm, Exemple : 12 : 00)");
                lblVeuillezRespecterCe.setFont(new Font("Trebuchet MS", Font.ITALIC, 12));
                lblVeuillezRespecterCe.setForeground(new Color(0, 128, 0));
                lblVeuillezRespecterCe.setBounds(26, 214, 428, 40);
                contentPane.add(lblVeuillezRespecterCe);
                
                lblSaisirHeureFin = new JLabel("Heure de fin   :");
                lblSaisirHeureFin.setBounds(76, 176, 131, 14);
                contentPane.add(lblSaisirHeureFin);
                
                txtHeureFin = new JTextField();
                txtHeureFin.setColumns(10);
                txtHeureFin.setBounds(216, 171, 39, 20);
                contentPane.add(txtHeureFin);
                
                txtMinFin = new JTextField();
                txtMinFin.setColumns(10);
                txtMinFin.setBounds(282, 171, 39, 20);
                contentPane.add(txtMinFin);
                
                label = new JLabel("  :");
                label.setFont(new Font("Tahoma", Font.PLAIN, 15));
                label.setBounds(256, 139, 23, 20);
                contentPane.add(label);
        }

        @Override
        public void actionPerformed(ActionEvent evt)
        {
            if(evt.getSource() == btnRetour)
            { this.dispose();}
            
            if(evt.getSource() == btnValider)
            {
            	if(this.txtHeureDeb.getText().isEmpty() || this.txtMinDeb.getText().isEmpty() ||  
            		 this.txtHeureFin.getText().isEmpty() || this.txtMinFin.getText().isEmpty())
    	        { 
    	        	JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs, merci");
    	        }
            	else
            	{
                	Entrainement unEntrainement;
                	int nb = 0;
                	
                	String heureDeb = txtHeureDeb.getText() + ":" + txtMinDeb.getText() + ":00";
                	String heureFin = txtHeureFin.getText() + ":" + txtMinFin.getText() + ":00";
                	
                	String date = cbAnnee.getSelectedItem() + "-" + cbMois.getSelectedItem() + "-" + cbJours.getSelectedItem();
                	int numAdherent = this.cbMoniteur.getSelectedIndex();
                	
                	unEntrainement = new Entrainement(0, date, heureDeb, heureFin, numAdherent);

                	System.out.println("la date : -> "+date);
                	System.out.println(numAdherent);
                	try
                	{
                		nb = dao.DaoEntrainement.ajouterEntrainement(unEntrainement);
                	}
                	catch(SQLException e)
                	{
                		e.printStackTrace();
                	}
                	catch(NumberFormatException ex)
                	{
                		ex.printStackTrace();
                	}
                	
                	if(nb == 0)
                	{
                		JOptionPane.showMessageDialog(null, "L'entrainement n'a pas pu être enregistré !", "Erreur",
                				JOptionPane.ERROR_MESSAGE);
                	}
                	else
	                {
	                	JOptionPane.showMessageDialog(null, "L'entrainement a été ajouté !","Confirmation", JOptionPane.INFORMATION_MESSAGE);
	                    this.lesEntrainements.add(unEntrainement);
	                    this.dispose();   
	                }
            	}
    	   }
       }
        
        //Transformation d'un format string en date anglaise   
//		public Date dateAng(String uneDateA)
//		{
//			Date uneDateAng = null;
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
//			
//			try
//			{
//				uneDateAng = sdf.parse(uneDateA);
//			} catch (ParseException e)
//			{
//				e.printStackTrace();
//			}
//			
//			return uneDateAng; 
//		}
        
        //Transformation d'un format string en time   
//		public Time time(Date uneHeure)
//		{
//			String sdf = new SimpleDateFormat("HH:mm:ss").format(uneHeure);
//			Time lHeure = null;
//			lHeure = Time.valueOf(uneHeure);
//			System.out.println(lHeure);
//			Date hr.setHeure(new SimpleDateFormat("HH:mm").parse(uneHeure));
//			return lHeure;
//		}
}

